<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="编号" prop="number">
        <el-input v-model="form.numberNum" disabled style="width: 3.55rem" />
      </el-form-item>
      <el-form-item label="调转人" prop="applicantName" class="textarea">
        <el-input
          v-model="form.applicantName"
          readonly
          :disabled="isdisabled"
          placeholder="请输入"
          style="width: 3.55rem"
        >
          <el-button
            type="primary"
            :disabled="isdisabled"
            slot="append"
            @click="openUserSelect"
            v-text="btnText"
          ></el-button>
          <el-button type="success" :disabled="isdisabled" slot="append" @click="clearUserSelect">清空</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="调动类型" prop="transferType">
        <el-select
          v-model="form.transferType"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in dict.type.transfer_type"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--            <el-form-item label="申请人" prop="applicantName">-->
      <!--              <el-input v-model="form.applicantName" :disabled='isdisabled' placeholder="请输入申请人" />-->
      <!--            </el-form-item>-->
      <!--            <el-form-item label="申请人ID" prop="applicantId">-->
      <!--              <el-input v-model="form.applicantId" :disabled='isdisabled' placeholder="请输入申请人ID" />-->
      <!--            </el-form-item>-->
      <el-form-item label="原部门" prop="currentDept">
        <el-select
          v-model="form.currentDept"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in deptList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          ></el-option>
        </el-select>
        <!--              <el-input v-model="form.currentDept" :disabled='isdisabled' placeholder="请输入原部门" />-->
      </el-form-item>
      <el-form-item label="原直属上级" prop="currentLeader">
        <el-input
          v-model="form.currentLeader"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请输入原直属上级"
        />
      </el-form-item>
      <el-form-item label="原职位" prop="currentPosition">
        <el-input
          v-model="form.currentPosition"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请输入原职位"
        />
      </el-form-item>
      <el-form-item label="原职级" prop="currentLevel">
        <el-select
          v-model="form.currentLevel"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in dict.type.rank_level"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <!--              <el-input v-model="form.currentLevel" :disabled='isdisabled' placeholder="请输入原职级" />-->
      </el-form-item>
      <el-form-item label="新部门" prop="newDept">
        <el-select
          v-model="form.newDept"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in deptList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="新直属上级" prop="newLeader">
        <el-input
          v-model="form.newLeader"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请输入新直属上级"
        />
      </el-form-item>
      <el-form-item label="新职位" prop="newPosition">
        <el-input
          v-model="form.newPosition"
          style="width: 3.55rem"
          :disabled="isdisabled"
          placeholder="请输入新职位"
        />
      </el-form-item>
      <el-form-item label="新职级" prop="newLevel">
        <el-select
          v-model="form.newLevel"
          :disabled="isdisabled"
          style="width: 3.55rem"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in dict.type.rank_level"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <!--              <el-input v-model="form.newLevel" :disabled='isdisabled' placeholder="请输入新职级" />-->
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="form.remarks"
          type="textarea"
          :disabled="isdisabled"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <div v-show="parentTaskId==null" class="dialog-footer" style="text-align:center">
      <el-button :loading="buttonLoading" size="small" type="info" @click="submitCallback">关闭</el-button>
      <el-button
        :loading="buttonLoading"
        size="small"
        type="primary"
        @click="submitForm"
        v-if="confirmVisible"
      >提交</el-button>
    </div>
    <!-- 工作流开始 -->
    <verify
      ref="verifyRef"
      :taskId="taskId"
      @submitCallback="submitCallback"
      :componentName="componentName"
      :taskVariables="taskVariables"
      :sendMessage="sendMessage"
    ></verify>
  </div>
</template>

<script>
import { getHumanTransfer } from "@/api/oa/humanTransfer";
import verify from "@/components/Process/Verify";
import { parseTime } from "@/utils/ruoyi";
import { listAll } from "@/api/system/dept";
export default {
  name: "HumanTransferForm",
  dicts: ["act_status", "rank_level", "transfer_type"],
  components: {
    verify
  },
  props: {
    businessKey: String, // 业务唯一标识
    parentTaskId: String, // 父级任务id
    taskId: String, // 任务id
    confirmVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      componentName: "岗位调转申请",
      isdisabled: true,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      btnText: "选择",
      isManager: false,
      propUserList: [],
      deptList: [],
      // 表单参数
      form: {},
      projectNameList: [],
      // 表单校验
      rules: {
        transferType: [
          { required: true, message: "调动类型不能为空", trigger: "change" }
        ],
        applicantName: [
          { required: true, message: "调转人不能为空", trigger: "blur" }
        ],
        currentDept: [
          { required: true, message: "原部门不能为空", trigger: "blur" }
        ],
        currentLeader: [
          { required: true, message: "原直属上级不能为空", trigger: "blur" }
        ],
        currentPosition: [
          { required: true, message: "原职位不能为空", trigger: "blur" }
        ],
        currentLevel: [
          { required: true, message: "原职级不能为空", trigger: "blur" }
        ],
        newDept: [
          { required: true, message: "新部门不能为空", trigger: "blur" }
        ],
        newLeader: [
          { required: true, message: "新直属上级不能为空", trigger: "blur" }
        ],
        newPosition: [
          { required: true, message: "新职位不能为空", trigger: "blur" }
        ],
        newLevel: [
          { required: true, message: "新职级不能为空", trigger: "blur" }
        ]
      },
      taskVariables: undefined,
      //消息提醒
      sendMessage: {},
      processKey: "oa_human_transfer"
    };
  },
  watch: {
    businessKey: {
      immediate: true, // 很重要！！！
      handler(newVal) {
        if (newVal) {
          this.getById();
        }
      }
    }
  },
  methods: {
    submitCallback() {
      this.$emit("closeForm");
    },
    async getById() {
      const { data } = await getHumanTransfer(this.businessKey);
      this.form = data;
      if (data.actBusinessStatus.createTime) {
        this.form.numberNum =
          this.processKey +
          "@" +
          parseTime(data.actBusinessStatus.createTime, "{y}{m}{d}{h}{i}{s}");
      }
      this.form.currentDept = Number(this.form.currentDept);
      this.form.newDept = Number(this.form.newDept);
      this.listAll();
    },
    /** 提交按钮 */
    submitForm() {
      getHumanTransfer(this.businessKey).then(response => {
        this.taskVariables = {
          entity: response.data
        };
        this.sendMessage = {
          title: "岗位调转申请审批提醒",
          messageContent:
            this.$store.state.user.nickName + "审批通过了岗位调转申请"
        };
      });
      this.$refs.verifyRef.visible = true;
      this.$refs.verifyRef.reset();
    },
    listAll() {
      listAll().then(res => {
        console.log(res.data);
        this.deptList = res.data;
      });
    },
    clearUserSelect() {
      this.form.applicantId = "";
      this.form.applicantName = "";
    },
    clickUser(userList) {
      let arrAssignee = userList.map(item => {
        return item.nickName;
      });
      let arrAssigneeId = userList.map(item => {
        return item.userId;
      });
      let resultAssignee = arrAssignee.join(",");
      let resultAssigneeId = arrAssigneeId.join(",");
      this.$set(this.form, "applicantName", resultAssignee);
      this.$set(this.form, "applicantId", resultAssigneeId);
      this.$refs.userRef.visible = false;
      this.$forceUpdate();
      // this.getUser(resultAssigneeId);
    },
    openUserSelect() {
      this.propUserList = [];
      if (this.form.applicantId) {
        let userIds = this.form.applicantId.split(",");
        if (userIds.length > 0) {
          this.propUserList = userIds;
        }
      }
      this.$refs.userRef.visible = true;
    }
  }
};
</script>
<style scoped>
/* 设置滚动条宽度和高度 */
::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 8px; /* 横向滚动条 */
  height: 8px; /* 纵向滚动条 必写 */
}
/* 设置滚动条样式 */
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #dde;
  border-radius: 3px;
}
#selectForm >>> .el-form-item__label {
  font-size: 12px;
}
::v-deep .bjsm .el-form-item__content {
  width: calc(100% - 130px) !important;
}
</style>
